import { createElementVNode, createTextVNode } from "./vDom/index";
import { patch } from "./vDom/patch";
import Watcher from "./observer/watcher";
export function initLifeCycle(Vue) {

    Vue.prototype._update = function (vNode) {
        let vm = this
        let el = vm.$el;
        const preVNode = vm._vNode // 记录上一次vNode
        vm._vNode = vNode
        if (preVNode) { // vNode被修改时
            vm.$el = patch(preVNode, vNode)
        } else { // 第一次

            vm.$el = patch(vm.$el, vNode)
        }

    }
    Vue.prototype._c = function () {
        return createElementVNode(this, ...arguments)
    }

    Vue.prototype._v = function () {
        return createTextVNode(...arguments)
    }

    Vue.prototype._s = function (value) {
        if (typeof value === 'string') return value
        return JSON.stringify(value)
    }

    Vue.prototype._render = function () {
        let vm = this
        return vm.$options.render.call(vm)
    }
}

export function mountComponent(vm) {

    function updateComponent() {
        let vNode = vm._render()
        vm._update(vNode)
    }
    let watcher = new Watcher(vm, updateComponent, true) // true 用来表示是否为渲染watcher
}

export function callHook(vm, hook) {
    if (Array.isArray(vm.$options[hook])) {
        vm.$options[hook].forEach(handle => handle.call(vm))
    }

}